package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ShenMin
 * @date 2021/7/15 11:41
 * @description
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<BrandDTO> listBrandByIds(List<Long> ids) {
        List<Brand> brandList = this.listByIds(ids);
        return BrandDTO.convertEntityList(brandList);
    }

    @Override
    public List<BrandDTO> listBrandByCategoryId(Integer categoryId) {
        List<Brand> brandList = brandMapper.listBrandByCategoryId(categoryId);
        if (CollectionUtils.isEmpty(brandList)){
            throw new LyException(400,"该类别暂无品牌");
        }
        return BrandDTO.convertEntityList(brandList);
    }

    @Override
    public PageDTO<BrandDTO> listBrandByPage(Integer page, Integer rows, String key) {
        page = Math.min(page, 100);
        rows = rows > 0 ? Math.max(rows, 5) : this.count();
        IPage<Brand> info = new Page<>(page, rows);
        boolean isKeyExists = StringUtils.isNoneBlank(key);
        query().like(isKeyExists, "name", key)
                .or()
                .eq(isKeyExists, "letter", key)
                .page(info);
        return new PageDTO<>(info.getTotal(), info.getPages(), BrandDTO.convertEntityList(info.getRecords()));
    }

    @Override
    public BrandDTO queryBrandById(Long id) {
        return new BrandDTO(this.query().eq("id", id).one());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = brandDTO.toEntity(Brand.class);
        List<CategoryBrand> categoryBrandList = new ArrayList<>();
        if (this.save(brand)) {
            categoryBrandList = cids
                    .stream()
                    .map(cid -> CategoryBrand.of(cid, brand.getId()))
                    .collect(Collectors.toList());
        }
        categoryBrandService.saveBatch(categoryBrandList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = brandDTO.toEntity(Brand.class);
        if (this.updateById(brand)) {
            this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", brand.getId()));
            List<CategoryBrand> categoryBrandList = cids
                    .stream()
                    .map(cid -> CategoryBrand.of(cid, brand.getId()))
                    .collect(Collectors.toList());
            categoryBrandService.saveBatch(categoryBrandList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBrand(Long id) {
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", id));
        this.removeById(id);
    }
}